package com.webService;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

/**
 * 获取URL内容
 * Created by IntelliJ IDEA.
 * User: zzx
 * Date: 2017/11/12
 * Time: 下午8:12
 */

public class GetURLcontent {
    public static void main(String[] args) throws IOException {
        //使用URL读取网页内容
        //创建一个URL实例
        URL url = new URL("http://localhost:8080");
        InputStream is = url.openStream();//通过openStream方法获取资源的字节输入流
        InputStreamReader isr = new InputStreamReader(is, "UTF-8");//将字节输入流转换为字符输入流,如果不指定编码，中文可能会出现乱码
        BufferedReader br = new BufferedReader(isr);//为字符输入流添加缓冲，提高读取效率
        String data = br.readLine();//读取数据
        if (data != null) {
            System.out.println(data);//输出数据
        }
        br.close();
        isr.close();
        is.close();
    }
}
